using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;
using BL;
using BE;

public partial class UsuarioAdmin : BasePage
{    
    #region Eventos
    protected void Page_Load(object sender, EventArgs e)
    {        
        txtNombre.Focus();

        if (!IsPostBack)
        {
            SetLabels();
            CargarUsuarios();
            btnEliminar.Attributes.Add("onclick", "return  window.confirm('" + Resources.Resource.msg_Eliminar + "')");
        }
    }
    protected void btnSalir_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx");
    }
    protected void btnAgregar_Click(object sender, EventArgs e)
    {
        Response.Redirect("UsuarioEdit.aspx");
    }
    protected void btnEliminar_Click(object sender, EventArgs e)
    {
        try
        {
            ArrayList arrayDelete = GetItemsCheckedInGrid();
            if (arrayDelete.Count > 0)
            {
                String strLogin = String.Empty;
                for (int i = 0; i < arrayDelete.Count; i++)
                {
                    Object[] args = (Object[])arrayDelete[i];
                    Int32 uidUsuarioId = Convert.ToInt32(args[0].ToString());
                    if (uidUsuarioId.ToString() == ((Usuario)Session[Constans.SessionUsuario.ToString()]).UsuarioId.ToString())
                        strLogin = args[1].ToString();
                    else
                        UsuarioBO.EliminarUsuario(uidUsuarioId);                    
                }
                CargarUsuarios();

                if (strLogin != String.Empty)
                {
                    String strMessage = String.Format(Resources.Resource.msg_EliminarUsuario, strLogin);
                    ScriptManager.RegisterStartupScript(UpdatePanelUsuario, GetType(), "Script", "alert('" + strMessage + "');", true);
                    return;
                }
            }
        }
        catch (ManagerBusinessException)
        {
            ShowErrorMessageInAjax(UpdatePanelUsuario);
        }
        catch (Exception ex)
        {
            Publish(ex, String.Empty);
            ShowErrorMessageInAjax(UpdatePanelUsuario);
        }
    }
   
    protected void gvUsuario_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "Edit":
                String strUsuarioId = e.CommandArgument.ToString();
                Response.Redirect("UsuarioEdit.aspx?UsuarioId=" + strUsuarioId);
                break;
        }    
    }
    
    protected void gvUsuario_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvUsuario.PageIndex = e.NewPageIndex;
        CargarUsuarios();
    }   
    protected void btnBuscar_Click(object sender, EventArgs e)
    {
        CargarUsuarios();
    }
    #endregion

    #region Metodos
    protected void SetLabels()
    {
        btnAgregar.Text = Resources.Resource.btnAgregar;
        btnEliminar.Text = Resources.Resource.btnEliminar;        
    }

    private void CargarUsuarios()
    {
        try
        {
            DataTable dtUsuarios = UsuarioBO.ObtenerUsuarios(txtNombre.Text.Trim());

            if (dtUsuarios.Rows.Count > 0)
            {
                pnlItems.Visible = true;
                pnlMensaje.Visible = false;
                gvUsuario.DataSource = dtUsuarios;
                gvUsuario.DataBind();
            }
            else
            {
                pnlItems.Visible = false;
                pnlMensaje.Visible = true;
            }
        }
        catch (ManagerBusinessException)
        {
            ShowErrorMessageInAjax(UpdatePanelUsuario);
        }
        catch (Exception ex)
        {
            Publish(ex, String.Empty);
            ShowErrorMessageInAjax(UpdatePanelUsuario);
        }
    }
    private ArrayList GetItemsCheckedInGrid()
    {
        ArrayList arrayList = new ArrayList();
        for (int i = gvUsuario.Rows.Count - 1; i >= 0; i--)
        {
            GridViewRow dgRow = gvUsuario.Rows[i];
            if (dgRow.RowType == DataControlRowType.DataRow)
            {                
                if (((CheckBox)dgRow.FindControl("chk")).Checked)
                    arrayList.Add(new Object[] { ((Label)dgRow.FindControl("lblUsuarioId")).Text, dgRow.Cells[1].Text});
            }
        }
        return arrayList;
    }
    #endregion           
}
